/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.soc.cs5226.gui.entities;

import edu.soc.cs5226.gui.entities.util.Constants;

import java.sql.Timestamp;
import java.util.Iterator;
import java.util.List;

/**
 *
 * @author huijia
 */
public class SharedPoolProcessList {
    public dayObject processTreeNode(Timestamp now, List<xObject> xList, HealthStatus hs)
    {
        long total24Number = 0;
        long total24PinHits = 0;
        long total24Pins = 0;
        long total24HitRatio = 0;
        
        Iterator<xObject> xIter = xList.iterator();
        while (xIter.hasNext())
        {
            long totalNumber = 0;
            long totalPinHits = 0;
            long totalPins = 0;
            long totalHitRatio = 0;
            xObject currX = xIter.next();
            Iterator<yObject> yIter = currX.getyObjects().iterator();
            while (yIter.hasNext())
            {
                long subtotalNumber = 0;
                long subtotalPinHits = 0;
                long subtotalPins = 0;
                long subtotalHitRatio = 0;
                yObject currY = yIter.next();
                Iterator<DBParameter> spIter = currY.getObj().iterator();
                while (spIter.hasNext())
                {
                    SharedPool spObj = (SharedPool)spIter.next();
                    total24PinHits = total24PinHits + spObj.getpinHits();
                    total24Pins = total24Pins + spObj.getpins();
                    total24HitRatio = total24HitRatio + spObj.gethitRatio();
                    total24Number++;
                    
                    totalPinHits = totalPinHits + spObj.getpinHits();
                    totalPins = totalPins + spObj.getpins();
                    totalHitRatio = totalHitRatio + spObj.gethitRatio();
                    totalNumber++;
                    
                    subtotalPinHits = subtotalPinHits + spObj.getpinHits();
                    subtotalPins = subtotalPins + spObj.getpins();
                    subtotalHitRatio = subtotalHitRatio + spObj.gethitRatio();
                    subtotalNumber++;
                }
                SharedPool treenodeY = (SharedPool)currY.getyObj();
                treenodeY.sethitRatio(calcAverage(subtotalHitRatio,subtotalNumber));
                treenodeY.setpinHits(calcAverage(subtotalPinHits,subtotalNumber));
                treenodeY.setpins(calcAverage(subtotalPins,subtotalNumber));
                treenodeY.setsps_healthStatus(processSharedPool(treenodeY, hs));
            }
            SharedPool treenodeX = (SharedPool)currX.getxObj();
            treenodeX.sethitRatio(calcAverage(totalHitRatio,totalNumber));
            treenodeX.setpinHits(calcAverage(totalPinHits,totalNumber));
            treenodeX.setpins(calcAverage(totalPins,totalNumber));
            treenodeX.setsps_healthStatus(processSharedPool(treenodeX,hs));
        }
        
        SharedPool treenodeRoot = (SharedPool)DBParamFactory.getDBParameter(Constants.SHAREDPOOL);
        treenodeRoot.sethitRatio(calcAverage(total24HitRatio,total24Number));
        treenodeRoot.setpinHits(calcAverage(total24PinHits,total24Number));
        treenodeRoot.setpins(calcAverage(total24Pins,total24Number));
        treenodeRoot.setsps_healthStatus(processSharedPool(treenodeRoot,hs));
        treenodeRoot.setdateTime(new Timestamp(now.getTime()-(24*60*60*1000)));
        
        dayObject dObject = new dayObject();
        dObject.setRootObj(treenodeRoot);
        dObject.setxObjects(xList);
        
        return dObject;
    }
    
    //--------------------------------------------------------------------------
    
    private long calcAverage(long value, long number)
    {
        if (value>0)
            return (value/number);
        else
            return 0;
    }
    
    //--------------------------------------------------------------------------
    // processSharedPoolSize obtains sharedpoolsize info from the database
    // and calculcates the status indicator
    // if the hitRatio > yellow indicator
    //    status is green.
    // if the hitRatio <= yellow & hitRatio > red indicator
    //    status is yellow
    // if the hitRatio <= red indicator
    //    status is red
    //--------------------------------------------------------------------------
    private String processSharedPool(SharedPool sp, HealthStatus hs)
    {
        String status = "G";
        if ((sp.gethitRatio()<=hs.getyellowThres()) && 
                (sp.gethitRatio()>hs.getredThres()))
        {
            status = "Y";
        }
        else if (sp.gethitRatio()<=hs.getredThres())
        {
            status = "R";
        }
        return status;
    } // processSharedPool
}
